package com.zxl.live.screen.screen.ui.helper.effect;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import com.zxl.live.screen.screen.constant.ICustomAction;
import com.zxl.live.screen.screen.ui.helper.data.SuiScreenDataHelper;
import com.zxl.live.screen.screen.ui.helper.window.WindowHelper;
import com.zxl.live.screen.screen.ui.service.ScreenService;
import com.zxl.live.screen.ui.activity.SplashActivity;
import com.zxl.live.tools.common.ApplicationHelper;


/**
 * Created by Administrator on 2018/1/23.
 */

public abstract class BaseEffectHelper implements SensorEventListener {

    public static String POSITION = "position";

    public static String DATATYPE = "dataType";

    public static String TIMER = "timer";

    protected Context mContext;

    private SensorManager mSensorManager;

    protected int position;

    protected SuiScreenDataHelper.DataType dataType;

    private OnScreenStatusChangeListener mOnScreenStatusChangeListener;

    public BaseEffectHelper(Context context) {
        mContext = context;
        mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    }

    public void setOnScreenStatusChangeListener(OnScreenStatusChangeListener mOnScreenStatusChangeListener) {
        this.mOnScreenStatusChangeListener = mOnScreenStatusChangeListener;
    }

    public boolean registerListener() {
        return mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        int sensorType = event.sensor.getType();         //获取传感器类型
        float[] values = event.values;          //values[0]:X轴，values[1]：Y轴，values[2]：Z轴
        if (sensorType == Sensor.TYPE_ACCELEROMETER) {           //如果传感器类型为加速度传感器，则判断是否为摇一摇
            if ((Math.abs(values[0]) > 17 || Math.abs(values[1]) > 17 || Math.abs(values[2]) > 17)) {
                mSensorManager.unregisterListener(this);
                onDismiss();
                if (null != mOnScreenStatusChangeListener) {
                    mOnScreenStatusChangeListener.onDismiss();
                }
            }
        }
    }

    /**
     * 初始化显示数据
     * @param timer
     * @return
     */
    protected abstract boolean initSuiData(final int timer);

    /**
     * 显示提示
     * @param timer
     */
    protected abstract void showTips(int timer);

    /**
     * 特效消失
     */
    public abstract void onDismiss();

    public void onStartCommand(Intent intent) {
        if (ICustomAction.ACTION_SUI_CHANGE.equals(intent.getAction())) {
            position = intent.getIntExtra(POSITION, 0);
            dataType = (SuiScreenDataHelper.DataType) intent.getSerializableExtra(DATATYPE);
            int timer = intent.getIntExtra(TIMER, 1);
            WindowHelper.getWindowHelper().removeAllView();
            if (initSuiData(timer)) {
                showTips(timer);
            } else {
                SplashActivity.startSplash(mContext, this.dataType, this.position);
            }
        }
    }




    public static void sendSuiShow(int position, SuiScreenDataHelper.DataType dataType, int timer) {
        Intent intent = new Intent(ApplicationHelper.getContext(), ScreenService.class);
        intent.setAction(ICustomAction.ACTION_SUI_CHANGE);
        intent.putExtra(POSITION, position);
        intent.putExtra(DATATYPE, dataType);
        intent.putExtra(TIMER, timer);
        ApplicationHelper.getContext().startService(intent);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }


    public interface OnScreenStatusChangeListener {

        void onDismiss();

    }
}
